package metrics

import dto "github.com/prometheus/client_model/go"

func GetTotalCpuMetricUsage(mf *dto.MetricFamily) float64 {
	var md = make(map[string]map[string]float64, 8)
	for _, m := range mf.Metric {
		var mode, cpu string
		for _, l := range m.Label {
			switch *l.Name {
			case "mode":
				mode = *l.Value
			case "cpu":
				cpu = *l.Value
			}
		}

		if _, ok := md[mode]; !ok {
			md[mode] = make(map[string]float64, len(mf.Metric)/8)
		}

		md[mode][cpu] = *m.Counter.Value
	}

	var idle float64
	for _, v := range md["idle"] {
		idle += v
	}

	var total float64
	for _, m := range md {
		for _, v := range m {
			total += v
		}
	}

	usage := 1.0 - (idle / total)

	return usage
}

func GetSpecialCpuMetricUsage(mf *dto.MetricFamily, cpus string) float64 {
	var md = make(map[string]float64, 8)
	for _, m := range mf.Metric {
		var mode, cpu string
		for _, l := range m.Label {
			switch *l.Name {
			case "mode":
				mode = *l.Value
			case "cpu":
				cpu = *l.Value
			}
		}

		if cpu == cpus {
			md[mode] = *m.Counter.Value
		}
	}

	var total float64
	for _, v := range md {
		total += v
	}

	usage := 1.0 - (md["idle"] / total)

	return usage
}
